﻿/*
--------------------------------------
File: FilterTest.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
Contains functions for checking the images for the amount of noise
Makes a calculation based on these numbers
--------------------------------------
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace THO7_Team8
{
    public partial class FilterTest : Form
    {
        List<Bitmap> myImageList;
        /* 
        --------------------------------------
        Author: Felix mann
        Description:
        Adds items from the provided imageList to the listViews. Also puts the image comments in the right place. 
        The GUI of this class is also written by me (:
        --------------------------------------
        */
        public FilterTest(ImageList il, List<String> operationList, List<Bitmap> imageList)
        {
            myImageList = imageList;
            InitializeComponent();
            listView1.LargeImageList = il;
            listView2.LargeImageList = il;
            listView3.LargeImageList = il;
            int counter = 0;
            foreach (String item in operationList)
            {
                listView1.Items.Add(item, counter);
                listView2.Items.Add(item, counter);
                listView3.Items.Add(item, counter);
                counter++;
            }
        }

        //Compares the original and the noised image for noise pixels and return the number of noise pixels
        public static double NoiseTest(Bitmap Original, Bitmap Noised)
        {
            int[] OrignalArray = MyImage.BmpToArray(Original);
            int[] NoisedArray = MyImage.BmpToArray(Noised);

            int width = Original.Width;
            int height = Original.Height;

            double Difference = 0;

            for (int i = 0; i < width * height; i++)
            {
                if (OrignalArray[i] != NoisedArray[i])
                {
                    Difference++;
                }
            }
            return Difference;
        }

        //Compares the original and the filtered image for noise pixels and returns the number of noise pixels
        public static double MedianTest(Bitmap Original, Bitmap Filtered, int filterSize)
        {
            int[] OrignalArray = MyImage.BmpToArray(Original);
            int[] NoisedArray = MyImage.BmpToArray(Filtered);

            int width = Original.Width;
            int height = Original.Height;

            double Difference = 0;

            //Skips the outer edges of the image, just like the median filter does
            for (int i = ((width) + 1) * (filterSize / 2); i < ((width) * height) - (width + 1) * (filterSize / 2); i++)
            {
                if (i % (width) < (filterSize / 2) || i % (width) == (width) - (filterSize / 2))
                {
                    continue;
                }

                //Checks each of the color channels for noise pixels
                //There is a small marge build in to ignore pixels that has been changed by the median filter
                if ((OrignalArray[i] & 255) - (NoisedArray[i] & 255) < -100 ||
                    (OrignalArray[i] & 255) - (NoisedArray[i] & 255) > 100)
                {
                    Difference++;
                }
                else if ((OrignalArray[i] >> 8 & 255) - (NoisedArray[i] >> 8 & 255) < -100 ||
                    (OrignalArray[i] >> 8 & 255) - (NoisedArray[i] >> 8 & 255) > 100)
                {
                    Difference++;
                }
                else if ((OrignalArray[i] >> 16 & 255) - (NoisedArray[i] >> 16 & 255) < -100 ||
                    (OrignalArray[i] >> 16 & 255) - (NoisedArray[i] >> 16 & 255) > 100)
                {
                    Difference++;
                }
            }
            return Difference;
        }

        private void cmdTest_Click(object sender, EventArgs e)
        {
            //Gets the bitmap images from the imagelist
            Bitmap original = new Bitmap(myImageList[listView1.FocusedItem.Index]);
            Bitmap noised = new Bitmap(myImageList[listView2.FocusedItem.Index]);
            Bitmap filtered = new Bitmap(myImageList[listView3.FocusedItem.Index]);

            //Gets the original pixels and altered pixels
            int originalPixels = original.Height * original.Width;
            double NoisePixels = NoiseTest(original, noised);
            double MedianPixels = MedianTest(original, filtered, 3);

            int filterSize = ((ExtendedForm)this.Owner.MdiParent).getVisionAlgorithmByName("MedianFilter")._configValue; //This line is now officially the most ugly line of code I have ever written. Or at least it's in the top 100. - Author: Felix Mann
            //Displays a message for the user
            MessageBox.Show("Median FilterSize: " + filterSize + "x" + filterSize + "\n" +
                            "Aantal pixels in het origineel: " + originalPixels + "\n\n" +
                            "Aantal toegevoegde ruis pixels: " + NoisePixels + "\n" +
                            "Dit is: " + ((NoisePixels / originalPixels) * 100).ToString("N2") + "% van het origineel" + "\n\n" +
                            "Aantal ruis pixels na median filter: " + MedianPixels + "\n" +
                            "Dit is " + ((MedianPixels / originalPixels) * 100).ToString("N2") + "% van het origineel", "Reultaat");                     
        }
    }
}
